﻿@{
    ViewBag.Title = "RestrictCharacter";
    Layout = "~/Views/Shared/_Page.cshtml";
}

<h2>Restrict Character</h2>

alphabet only:
<input id="myInput1" type="text" class="inputbox" />
<br />

number only:
<input id="myInput2" type="text" class="inputbox" />
<br />

alphabet & number only:
<input id="myInput3" type="text" class="inputbox" />

<script type="text/javascript">
    $(document).ready(function () {
        // alphabet only
        $('#myInput1').keypress(function (event) {
            var regex = new RegExp("^[a-zA-Z]+$");
            var key = String.fromCharCode(event.which);
            if (!regex.test(key)) {
                return false;
            }
        });

        // number only
        $('#myInput2').keypress(function (event) {
            var regex = new RegExp("^[0-9]+$");
            var key = String.fromCharCode(event.which);
            if (!regex.test(key)) {
                return false;
            }
        });

        // alphabet & number only
        $('#myInput3').keypress(function (event) {
            var regex = new RegExp("^[a-zA-Z0-9]+$");
            var key = String.fromCharCode(event.which);
            if (!regex.test(key)) {
                return false;
            }
        });
    });
</script>